package com.ruoyi.lh.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.lh.domain.Machine;
import com.ruoyi.lh.mapper.MachineMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.lh.mapper.ProjectMapper;
import com.ruoyi.lh.domain.Project;
import com.ruoyi.lh.service.IProjectService;
import com.ruoyi.common.core.text.Convert;
import org.springframework.util.CollectionUtils;

/**
 * 项目Service业务层处理
 *
 * @author LH
 * @date 2023-08-01
 */
@Service
public class ProjectServiceImpl implements IProjectService {
  @Autowired private ProjectMapper projectMapper;

  @Autowired private MachineMapper machineMapper;

  /**
   * 查询项目
   *
   * @param id 项目主键
   * @return 项目
   */
  @Override
  public Project selectProjectById(Long id) {
    return projectMapper.selectProjectById(id);
  }

  /**
   * 查询项目列表
   *
   * @param project 项目
   * @return 项目
   */
  @Override
  public List<Project> selectProjectList(Project project) {
    return projectMapper.selectProjectList(project);
  }

  /**
   * 新增项目
   *
   * @param project 项目
   * @return 结果
   */
  @Override
  public int insertProject(Project project) {
    project.setCreateTime(DateUtils.getNowDate());
    int count = projectMapper.insertProject(project);
    if (null != project.getMachineList() && !project.getMachineList().isEmpty()) {
      for (Machine machine : project.getMachineList()) {
        machine.setProjectId(project.getId() + "");
        machine.setProjectName(project.getProjectName());
        machine.setProjectNo(project.getProjectNo());
        machineMapper.insertMachine(machine);
      }
    }

    return count;
  }

  /**
   * 修改项目
   *
   * @param project 项目
   * @return 结果
   */
  @Override
  public int updateProject(Project project) {
    project.setUpdateTime(DateUtils.getNowDate());
    int count = projectMapper.updateProject(project);
    if (null != project.getMachineList() && !project.getMachineList().isEmpty()) {
      for (Machine machine : project.getMachineList()) {
        machine.setProjectId(project.getId() + "");
        machine.setProjectName(project.getProjectName());
        machine.setProjectNo(project.getProjectNo());
        machineMapper.insertMachine(machine);
      }
    }
    return count;
  }

  /**
   * 批量删除项目
   *
   * @param ids 需要删除的项目主键
   * @return 结果
   */
  @Override
  public int deleteProjectByIds(String ids) {
    return projectMapper.deleteProjectByIds(Convert.toStrArray(ids));
  }

  /**
   * 删除项目信息
   *
   * @param id 项目主键
   * @return 结果
   */
  @Override
  public int deleteProjectById(Long id) {
    return projectMapper.deleteProjectById(id);
  }

  @Override
  public List<String> selectProjectNoList() {
    return projectMapper.selectProjectNoList();
  }

  @Override
  public List<Machine> selectMachineList(Long id) {
    return projectMapper.selectMachineList(id);
  }
}
